//
//  PKReadInfoViewController.m
//  PianKe
//
//  Created by ybq on 15/12/29.
//  Copyright © 2015年 B7_Young. All rights reserved.
//

#import "PKReadInfoViewController.h"
#import "PKReadInfoModel.h"

@interface PKReadInfoViewController () <UIWebViewDelegate>

@property (nonatomic, strong) PKReadInfoModel *model;
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation PKReadInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.
    UIView *stateView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    stateView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:stateView];
    [self requestData];
    [self creatUI];
}

- (void)requestData {
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"07EBFC18-9BF8-47D1-9FEA-79BBF44D4C9E";
    parDic[@"version"] = @"3.0.6";
    parDic[@"contentid"] = _contentid;
    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/article/info" parDic:parDic Method:POST finish:^(NSData *data) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *infoDic = dataDic[@"data"];
        _model = [[PKReadInfoModel alloc] init];
        [_model setValuesForKeysWithDictionary:infoDic];
        [self loadWebView];
    } error:^(NSError *error) {
        
    }];
}

- (void)creatUI {
    PKNavigationView *navView = [[PKNavigationView alloc] initWithFrame:CGRectMake(0, 20, kWidth, 45)];
    [navView.leftButton setImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [navView.leftButton addTarget:self action:@selector(backAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:navView];
    

}

- (void)loadWebView {
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, kWidth, kHeight - 64)];
    NSString *str = [NSString stringWithFormat:@"%@",_model.html];
    str = [NSString stringWithFormat:@"<head><style>img{width:%fpx !important;}</style></head>%@",kWidth - 20 ,str];
    [_webView loadHTMLString:str baseURL:nil];
    _webView.delegate = self;
    [self.view addSubview:_webView];
}

// 这个代理方法控制了我们的UIWebView是否能够加载,返回YES代表可以加载,返回NO代表不能加载(UIWebView每次加载一个新的界面的时候都会执行这个方法)
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//    // 从NSURLRequest获取到url
//    NSURL *url = request.URL;
//    // 从NSURL里面获取字符串
//    NSString *str = url.absoluteString;
//    NSLog(@"%@",str);
//    // 根据字符串来判断是否加载这个请求
//    if ([str isEqualToString:urlStr]) {
//        return YES;
//    }
//    // 通过字符串获取到一个SEL类型的对象 (SEL表示的是一个方法的ID)
//    str = @"fun:openCamera";
//    SEL sss = NSSelectorFromString([str stringByReplacingOccurrencesOfString:@"fun:" withString:@""]);
//    // 让self去执行这个方法
//    [self performSelector:sss withObject:nil];
//    return NO;
//}

// UIWebView每次加载完之后就会执行这个代理方法

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    // 使用webView去执行一段JS的脚本的代码,通过这个代码我们可以返回一个字符串
    CGFloat height = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"] floatValue];
    NSLog(@"%f",height);
}

- (void)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
